package com.briup.prs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

import java.io.IOException;

public class Step8 extends Configured implements Tool {
    public static class Step8Mapper extends Mapper<LongWritable, Text, Text, Text> {
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            Text outKey = new Text();
            Text outValue = new Text();
            String s = value.toString();
//           确认数据 来源于用户行为数据
            if (s.contains(",")) {
                String[] arr = s.split("[,]");
                if ("paySuccess".equals(arr[2])) {
                    outKey.set(arr[0] + ":" + arr[1]);
                    outValue.set("ok");
                }else {
                    return;
                }
//                数据来源是推荐数据的时候
            } else {
                String[] arr = s.split("[\t]");
                outKey.set(arr[0]);
                outValue.set(arr[1]);
            }

            context.write(outKey, outValue);
        }
    }

    public static class Step8Reducer extends Reducer<Text,Text,Text,Text> {

        @Override
        protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
            Text outVal=new Text();
            for (Text value : values) {
                String val = value.toString();
//                表示已经有购买行为
                if ("ok".equals(val)){
//                    直接结束方法 不会有数据输出
                    return;
                }else{
                    outVal.set(val);
                }
            }
            System.out.println("key = " + key.toString());
            System.out.println("outVal = " + outVal.toString());
            context.write(key,outVal);
        }
    }

    @Override
    public int run(String[] args) throws Exception {
        Configuration conf = getConf();
        String in1 = conf.get("in1");

        String in2 = conf.get("in2");

        String out = conf.get("out");


        Job job = Job.getInstance(conf);
        job.setJobName("step8");
        job.setJarByClass(this.getClass());

        TextInputFormat.setInputPaths(job,new Path(in1),new Path(in2));
        job.setMapperClass(Step8Mapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);

        job.setReducerClass(Step8Reducer.class);
        job.setOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);

        TextOutputFormat.setOutputPath(job,new Path(out));

        return job.waitForCompletion(true)? 0:-1;
    }

    public static void main(String[] args) throws Exception {
        ToolRunner.run(new Step8(), args);
    }
}
